import subprocess
from subprocess import PIPE
import re


class SingleTester:


    def __init__(self, userCodePath, testPath):
        self.userCodePath = userCodePath
        self.testPath = testPath
        
    def executeTest(self): 
        try:
            testSubProcess = subprocess.Popen(['sh', '/home/psyburn/workspace/Projekt/src/Projekt/UnitTestSystem/test_executor.sh', self.testPath], stdout=PIPE).communicate()[0]
        except:
            Exception("What's wrong with this computer")
            return -1
         
        testsPassed = re.findall(r"Tests run: (\d+)", testSubProcess)
        if not testsPassed:
            testsPassed = 0
        else:
            testsPassed = int(testsPassed[0])
            
        testsFailed = re.findall(r"Failures: (\d+)", testSubProcess)
        
        if not testsFailed:
            testsFailed = 0
        else:
            testsFailed = int(testsFailed[0])
            
        return {'passed' : testsPassed, 'failed' : testsFailed}
'''
userTestPath = '/home/psyburn/workspace/Projekt/src/Projekt/uploads/user/MySimpleClass'
testTestPath = 'Testeric'
test = SingleTester(userTestPath, testTestPath)
x = test.executeTest()

print "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
print x
'''